#include "vga.h"
#include "printk.h"
/*vga memory address*/
unsigned char *vga_mem = (unsigned char *)0xb8000;

unsigned char vga_show_arr[] = "starting kernel...";

static int vga_pos = 0;

void clear_screen(void)
{
    int i = 0 ;
    for(i = 0 ;i  < VGA_WIDTH * VGA_HEIGHT ; i ++){
        *(vga_mem + 2 * i  + 0) = 0;
        *(vga_mem + 2 * i  + 1) = 0;
    }
}

void show_banner(void)
{
    int i = 0 ;
    for(i = 0 ;i  < sizeof(vga_show_arr)/sizeof(unsigned char) ; i ++){
        *(vga_mem + 2 * i  + 0) = vga_show_arr[i];
        *(vga_mem + 2 * i  + 1) = 0x42;

        vga_pos += 2;
    }
}
 
void _vga_show_char(unsigned char val)
{
    int line;
    if(val=='\n'){
        line = vga_pos/(VGA_WIDTH*2);
        line = line+1;
        vga_pos = (VGA_WIDTH*2) * line;
        return;
    }
    *(vga_mem + vga_pos + 0) = val;
    *(vga_mem + vga_pos + 1) = 0x42;  
    vga_pos += 2; 
    vga_pos =  vga_pos % (VGA_WIDTH * VGA_HEIGHT * 2);
}

void vga_show_string(unsigned char *val)
{
    while(*val){
        _vga_show_char(*val);
        val ++;
    }
}

#define VGA_HINT "> "



void vga_show_hint(void)
{
    vga_show_string(VGA_HINT);
}

void vga_show_char_regex(unsigned char val)
{
    int line;
    if(val=='\n'){
        line = vga_pos/(VGA_WIDTH*2);
        line = line+1;
        vga_pos = (VGA_WIDTH*2) * line;
        vga_show_hint();
        return;
    }
    _vga_show_char(val);
}
  
void vga_show_hex(unsigned char val)
{
    int i;
    unsigned char hex_val[2]={0};//0 high 1 low
    hex_val[0] = (val>>4)&0xf;
    hex_val[1] = (val)&0xf;

    for(i = 0 ;i < 2 ; i ++){
        if(hex_val[i]>=0xa)
            hex_val[i] = hex_val[i] - 10 + 'a'; 
        else
            hex_val[i] = hex_val[i] + '0';
        
        *(vga_mem + vga_pos + 0) = hex_val[i];
        *(vga_mem + vga_pos + 1) = 0x42;
        vga_pos += 2; 
        vga_pos =  vga_pos % (VGA_WIDTH * VGA_HEIGHT * 2);
    }
}
 
int init_vga(void)
{
    clear_screen();    
    //show_banner();
    //test_printk();
    printk("%s",vga_show_arr);
    return 0;
}

void test_printk()
{
    int a = 123451,b=0x7744;
    printk("a:%d,%d",a,-7872);
    printk("0x%x",0x1234);
    printk("b:0x%x",b);

    printk("show string:%s","hello wrold....");
}








